package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-12 09:32
 * @notify
 * @version 1.0
 */

import com.datang.pet.mapper.ShoppingCarMapper;
import com.datang.pet.mapperExtend.GoodsMapperExtend;
import com.datang.pet.pojo.ShoppingCar;
import com.datang.pet.pojo.ShoppingCarExample;
import com.datang.pet.service.ShoppingCarService;
import com.datang.pet.vo.GetShoppingCarList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private GoodsMapperExtend goodsMapperExtend;

    /*
     * @author 顶风少年
     * @date 2019/7/12 9:47
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param shoppingCar
     * @result void
     * @notify 添加商品到购物车
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public  List<GetShoppingCarList>  addGoods(ShoppingCar shoppingCar) throws Exception {
        shoppingCar.setCreateDate(new Date());
        try {
            shoppingCarMapper.insert(shoppingCar);
        } catch (Exception e) {
            throw new Exception("添加商品到购物车失败");
        }
        return  getShoppingCarList(shoppingCar.getUserId());
    }


    /*
     * @author 顶风少年
     * @date 2019/7/12 9:47
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param goodsId
     * @param userId
     * @result void
     * @notify 删除商品从购物车
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public  List<GetShoppingCarList>  deleteGoods(List<String> specId, String userId) throws Exception {
        ShoppingCarExample shoppingCarExample = new ShoppingCarExample();
        ShoppingCarExample.Criteria criteria = shoppingCarExample.createCriteria();
        criteria.andSpecIn(specId);
        criteria.andUserIdEqualTo(userId);
        try {
            shoppingCarMapper.deleteByExample(shoppingCarExample);
        } catch (Exception e) {
            throw new Exception("从购物车删除商品失败");
        }
        return  getShoppingCarList(userId);
    }


    /*
     * @author 顶风少年
     * @date 2019/7/12 9:50
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param shoppingCar
     * @result void
     * @notify 更改购车车商品数量
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public  List<GetShoppingCarList>  updateGoodsQuantity(ShoppingCar shoppingCar) throws Exception {
        ShoppingCarExample shoppingCarExample = new ShoppingCarExample();
        ShoppingCarExample.Criteria criteria = shoppingCarExample.createCriteria();
        criteria.andUserIdEqualTo(shoppingCar.getUserId());
        criteria.andGoodsIdEqualTo(shoppingCar.getGoodsId());
        criteria.andSpecEqualTo(shoppingCar.getSpec());
        try {
            shoppingCarMapper.updateByExampleSelective(shoppingCar, shoppingCarExample);
        } catch (Exception e) {
            throw new Exception("更改购物车数量失败");
        }
        return  getShoppingCarList(shoppingCar.getUserId());
    }


    /*
     * @author 顶风少年
     * @date 2019/7/12 10:33
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<com.datang.pet.vo.GetShoppingCarList>
     * @notify 获取购物车内商品
     * version 1.0
     */
    public List<GetShoppingCarList> getShoppingCarList(String userId) throws Exception {
        try {
            return goodsMapperExtend.getShoppingCarList(userId);
        } catch (Exception e) {
            throw new Exception("获取购物车商品失败");
        }

    }
}
